﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sunboxsoft.Common;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using System.Data.SQLite;

namespace Sunboxsoft.ASMS.Client.Domain.LocalService
{
    public class GetService
    {
        /// <summary>
        /// 获取主数据信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public SingleResult<WebRef.EMasterData> GetMasterData(string code)
        {
            using (SQLiteConnection connection = new SQLiteConnection(SQLiteDatabase.ConnectionString))
            {
                connection.Open();

                SQLiteCommand command = new SQLiteCommand(connection);
                command.CommandText = "select * from ASMS_MasterData where [Code]=@Code";
                SQLiteParameter pCode = command.CreateParameter();
                pCode.ParameterName = "@Code";
                pCode.Value = code;
                command.Parameters.Add(pCode);

                SQLiteDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    WebRef.EMasterData entity = new WebRef.EMasterData();
                    entity.Code = Convert.ToString(reader["Code"]);
                    entity.Name = Convert.ToString(reader["Name"]);
                    entity.Pinyin = Convert.ToString(reader["Pinyin"]);
                    entity.Letter = Convert.ToString(reader["Letter"]);
                    entity.Changeset = Convert.ToInt32(reader["Changeset"]);

                    reader.Close();
                    return new SingleResult<WebRef.EMasterData>(entity);
                }
                reader.Close();
                return new SingleResult<WebRef.EMasterData>(1, "no data");
            }
        }

        public SingleResult<WebRef.EMasterData> GetMasterData(SQLiteConnection connection, string code)
        {
            SQLiteCommand command = new SQLiteCommand(connection);
            command.CommandText = "select * from ASMS_MasterData where [Code]=@Code";
            SQLiteParameter pCode = command.CreateParameter();
            pCode.ParameterName = "@Code";
            pCode.Value = code;
            command.Parameters.Add(pCode);

            SQLiteDataReader reader = command.ExecuteReader();
            if (reader.Read())
            {
                WebRef.EMasterData entity = new WebRef.EMasterData();
                entity.Code = Convert.ToString(reader["Code"]);
                entity.Name = Convert.ToString(reader["Name"]);
                entity.Pinyin = Convert.ToString(reader["Pinyin"]);
                entity.Letter = Convert.ToString(reader["Letter"]);
                entity.Changeset = Convert.ToInt32(reader["Changeset"]);

                reader.Close();
                return new SingleResult<WebRef.EMasterData>(entity);
            }
            reader.Close();
            return new SingleResult<WebRef.EMasterData>(1, "no data");
        }
    }
}
